package com.eric.webproject.filter;


import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@WebFilter(filterName = "LoginFilter",urlPatterns = {"/*"})
public class LoginFilter extends HttpFilter {


    private String[] excludes = {"/","/common/login","/common/login.html","/common/exit"
            ,"/common/timeout.html","/main1.html"};
    private String[] excludes2 = {"/js/","/css/","/images/","/layui/","/common/verifyCode"};


    @Override
    public void doFilter(HttpServletRequest request,HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
        String path = request.getServletPath();
//        System.out.println(path);
        for(String exclude : excludes){
            if(path.equals(exclude)){
                chain.doFilter(request,response);
                return;
            }
        }
        for(String exclude : excludes2){
            if(path.startsWith(exclude)){
                chain.doFilter(request,response);
                return;
            }
        }

        HttpSession session = request.getSession();
        Object loginTeacher = session.getAttribute("loginTeacher");
        if(loginTeacher!=null){
            //如果验证通过，就放过请求，继续完成这次的请求操作
            chain.doFilter(request,response);
        }else {
            request.getRequestDispatcher("/common/timeout.html").forward(request, response);
        }
    }
}
